package annotation

import (
	"strings"

	"github.com/spf13/cast"
)

type Type uint32

const (
	Type_Sign Type = iota
	Type_Version
	Type_Key
)

var (
	Type_name = map[Type]string{
		Type_Sign:    "sign",
		Type_Version: "ver",
		Type_Key:     "key",
	}

	Name_type = map[string]Type{
		"sign": Type_Sign,
		"ver":  Type_Version,
		"key":  Type_Key,
	}
)

type Annotation interface {
	Name() string
	Type() Type
	String() string
	Int() int
	Bool() bool
}

type BaseAnnotation struct {
	atype Type
	value interface{}
}

func (a *BaseAnnotation) Name() string {
	return Type_name[a.atype]
}

func (a *BaseAnnotation) Type() Type {
	return a.atype
}

func (a *BaseAnnotation) String() string {
	return cast.ToString(a.value)
}

func (a *BaseAnnotation) Int() int {
	return cast.ToInt(a.value)
}

func (a *BaseAnnotation) Bool() bool {
	return cast.ToBool(a.value)
}

type Annotations map[Type][]Annotation

func (as Annotations) Get(t Type) []Annotation {
	return as[t]
}

func (as Annotations) GetByName(n string) []Annotation {
	t, ok := Name_type[strings.TrimSpace(n)]
	if ok {
		return as[t]
	}
	return nil
}
